﻿using System;
using DevExpress.XtraEditors;

namespace CSW.Framework.UI.DevEx.Forms
{
    /// <summary>
    /// DXUserNamePasswordForm.
    /// </summary>
    public partial class DXUserNamePasswordForm : DXBaseEditForm
    {
        private char m_PasswordChar;

        /// <summary>
        /// Initializes a new instance of the <see cref="DXUserNamePasswordForm"/> class.
        /// </summary>
        public DXUserNamePasswordForm()
        {
            InitializeComponent();

            PasswordChar = char.ConvertFromUtf32(0x2022)[0];
            HidePassword();
        }

        /// <summary>
        /// Gets or sets the password char.
        /// </summary>
        /// <value>The password char.</value>
        public char PasswordChar
        {
            get { return m_PasswordChar; }
            set
            {
                if (value == '\0')
                    throw new NotSupportedException(@"'\0' is not a supported password character.");

                m_PasswordChar = value;
                if (txtPassword.Properties.PasswordChar != '\0')
                    txtPassword.Properties.PasswordChar = m_PasswordChar;
            }
        }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName
        {
            get { return txtUserName.Text; }
            set { txtUserName.Text = value; }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }

        /// <summary>
        /// Gets the user name label control.
        /// </summary>
        /// <value>The user name label control.</value>
        public LabelControl UserNameLabelControl
        {
            get { return lblUserName; }
        }

        /// <summary>
        /// Gets the password label control.
        /// </summary>
        /// <value>The password label control.</value>
        public LabelControl PasswordLabelControl
        {
            get { return lblPassword; }
        }

        /// <summary>
        /// Gets the user name text edit.
        /// </summary>
        /// <value>The user name text edit.</value>
        public TextEdit UserNameTextEdit
        {
            get { return txtUserName; }
        }

        /// <summary>
        /// Gets the password text edit.
        /// </summary>
        /// <value>The password text edit.</value>
        public TextEdit PasswordTextEdit
        {
            get { return txtPassword; }
        }

        /// <summary>
        /// Gets the show password check edit.
        /// </summary>
        /// <value>The show password check edit.</value>
        public CheckEdit ShowPasswordCheckEdit
        {
            get { return chkShowPassword; }
        }

        private void chkShowPassword_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShowPassword.Checked)
                ShowPassword();
            else
                HidePassword();
        }

        /// <summary>
        /// Shows the password.
        /// </summary>
        public void ShowPassword()
        {
            if (!chkShowPassword.Checked)
                chkShowPassword.Checked = true;
            else
                txtPassword.Properties.PasswordChar = '\0';
        }

        /// <summary>
        /// Hides the password.
        /// </summary>
        public void HidePassword()
        {
            if (chkShowPassword.Checked)
                chkShowPassword.Checked = false;
            else
                txtPassword.Properties.PasswordChar = m_PasswordChar;
        }

        /// <summary>
        /// Focuses the password text edit.
        /// </summary>
        public void FocusPasswordTextEdit()
        {
            txtPassword.TabIndex = 0;
            chkShowPassword.TabIndex = 1;
            btnSave.TabIndex = 2;
            btnCancel.TabIndex = 3;
            txtUserName.TabIndex = 4;
            txtPassword.Focus();
        }
    }
}
